﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Agnes.Core;

namespace Agnes.MyDocsLib
{
    public class DeleteCatalogCmd
    {
        private static DeleteCatalogCmd _instance;

        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static DeleteCatalogCmd Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DeleteCatalogCmd();
                }
                return _instance;
            }
        }

        private RoutedCommand _requery;

        /// <summary>
        /// 是否能执行
        /// </summary>
        public bool CanExcuted { get; set; }

        DeleteCatalogCmd()
        {
            CanExcuted = true;
            _requery = new RoutedCommand("Delete Catalog Cmd", typeof(DeleteCatalogCmd));
        }

        /// <summary>
        /// 文章编辑模式的路由命令
        /// </summary>
        public static RoutedCommand Requery
        {
            get
            {
                return Instance._requery;
            }
        }

        /// <summary>
        /// 执行代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                Catalog selCatalog = MyDocsLibManager.Instance.ArticleManager.SelectedNode as Catalog;
                if (MessageBox.Show("将会删除目录: " + selCatalog.Title + "包括下面的文章，确定要删除该目录吗？",
                    "提醒", MessageBoxButton.YesNo, MessageBoxImage.Warning)
                    == MessageBoxResult.Yes)
                {
                    MyDocsLibManager.Instance.ArticleManager.DelCatalog(selCatalog);
                    MyDocsLibManager.Instance.ArticleManager.SelectedNode = null;
                }
            }
            catch (Exception ex)
            {
                AgnesServiceManager.LogServ.Error("MyDocsLib--删除目录失败，错误：" + ex.Message);
            }
        }

        /// <summary>
        /// 判断是否能执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (AgnesServiceManager.AgnesSlnService.CurrentSln == null 
                || !(MyDocsLibManager.Instance.ArticleManager.SelectedNode is Catalog))
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
        }
    }
}
